package com.example.leetcode.c1_c100.c49;

import java.util.*;

/**
 * 字母异位词分组
 * @author zhanpengguo
 * @date 2020-09-13 15:02
 */
public class Solution {

    public static void main(String[] args) {
        Solution solution = new Solution();
        solution.groupAnagrams(new String[]{"eat", "tea", "tan", "ate", "nat", "bat"});
    }

    public List<List<String>> groupAnagrams(String[] strs) {
        Map<String, List<String>> map = new HashMap<>();
        for (String str : strs) {
            char[] chars = str.toCharArray();
            Arrays.sort(chars);
            String newStr = new String(chars);
            List<String> strings = map.get(newStr);
            if (strings == null) {
                strings = new ArrayList<>();
            }
            strings.add(str);
            map.put(newStr, strings);
        }
        return new ArrayList<>(map.values());
    }
}
